package com.test.lyt.uils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.List;


public class RestTemplateUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateUtils.class);


    public static <T> Response<T> postForResponse(RestTemplate restTemplate, String url, Object request,
                                                  Class<T> cls, Object... urlVariables) {
        ResponseEntity<String> entity =
                restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);
        return JSON.parseObject(entity.getBody(), new TypeReference<Response<T>>(cls) {
        }.getType());
    }


    public static Response postForResponse(RestTemplate restTemplate, String url, Object request,
                                           Object... urlVariables) {
        ResponseEntity<String> entity =
                restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);
        return JSON.parseObject(entity.getBody(), Response.class);
    }


    public static <T> Response<PageResult<T>> postForResponsePageResult(RestTemplate restTemplate, String url,
                                                                        Object request,
                                                                        Class<T> cls, Object... urlVariables) {
        ResponseEntity<String> entity =
                restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);

        return JSON.parseObject(entity.getBody(),
                new TypeReference<Response<PageResult<T>>>(cls) {
                }.getType());
    }


    public static <T> Response<List<T>> postForResponseList(RestTemplate restTemplate, String url, Object request,
                                                            Class<T> cls, Object... urlVariables) {

        ResponseEntity<String> entity =
                restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);

        return JSON.parseObject(entity.getBody(), new TypeReference<Response<List<T>>>(cls) {
        }.getType());
    }


    public static <T> T postForObject(RestTemplate restTemplate, String url, Object request,
                                      Class<T> cls, Object... urlVariables) {

        ResponseEntity<String> entity =
                restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables);

        return JSON.parseObject(entity.getBody(), cls);
    }


    public static <T> T postForObject(RestTemplate restTemplate, String url, HttpEntity<?> requestEntity,
                                      Class<T> cls, Object... urlVariables) {

        ResponseEntity<String> entity =
                restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class, urlVariables);
        return JSON.parseObject(entity.getBody(), cls);
    }


    public static <T> T getForObject(RestTemplate restTemplate, String url, Class<T> cls) {
        ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
        return JSON.parseObject(entity.getBody(), cls);
    }


    public static <T> Response<T> getForResponse(RestTemplate restTemplate, String url, Class<T> cls) {
        ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
        return JSON.parseObject(entity.getBody(), new TypeReference<Response<T>>(cls) {
        }.getType());
    }
}
